home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Microsoft / Expression Blend / Blend.en.msi / Sparkle.GPiano.Knob.xaml.cs.en < prev    next >
Encoding:
Text File  |  2007-03-19  |  10.5 KB  |  177 lines

  1.  ■using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Animation;
  9. using System.Windows.Navigation;
  10. using System.Windows.Input;
  11. using System.ComponentModel;
  12. namespace GrandPiano
  13. {
  14.     /// <summary>
  15.     /// INotifyPropertyChanged allows that properties of the Class Knob
  16.     /// participate as source in data bindings.
  17.     /// </summary>
  18.     public partial class Knob : INotifyPropertyChanged
  19.     {
  20.         /// <summary>
  21.         /// INotifyPropertyChanged requires a property called PropertyChanged.
  22.         /// </summary>
  23.         public event PropertyChangedEventHandler PropertyChanged;
  24.         private double angle;
  25.         private double previousAngle;
  26.         private double currentAngle;
  27.         private double minimum;
  28.         private double maximum;
  29.         private double value;
  30.         #region Public Attributes
  31.         /// <summary>
  32.         /// These public attibutes are exposed in Expression Blend under Miscellaneous category in the Properties panel.
  33.         /// </summary>
  34.         public double Minimum
  35.         {
  36.             set { this.minimum = value; }
  37.             get { return this.minimum; }
  38.         }
  39.         public double Maximum
  40.         {
  41.             set { this.maximum = value; }
  42.             get { return this.maximum; }
  43.         }
  44.         public double Value
  45.         {
  46.             set
  47.             {
  48.                 this.value = value;
  49.                 if (this.PropertyChanged != null)
  50.                 {
  51.                     // Fires the event when the property Value changes.
  52.                     this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));
  53.                 }
  54.             }
  55.             get { return this.value; }
  56.         }
  57.         #endregion
  58.         #region Initialization
  59.         public Knob()
  60.         {
  61.             this.InitializeComponent();
  62.         }
  63.         protected override void OnInitialized(EventArgs e)
  64.         {
  65.             base.OnInitialized(e);
  66.         }
  67.         /// <summary>
  68.         /// This method is triggered only when all the elements are ready.
  69.         /// </summary>
  70.         private void OnLoaded(object sender, RoutedEventArgs e)
  71.         {
  72.             UpdateKnobRotation();
  73.         }
  74.         /// <summary>
  75.         /// The first time the Knob is loaded, this method updates the angle based on the porperty value.
  76.         /// </summary>
  77.         private void UpdateKnobRotation()
  78.         {
  79.             if (this.minimum < 0)
  80.             {
  81.                 angle = this.value * 130;
  82.             }
  83.             else
  84.             {
  85.                 double middle = this.maximum / 2;
  86.                 double getValue = (this.Value - middle) / middle;
  87.                 angle = getValue * 130;
  88.             }
  89.             rotateKnob.Angle = angle - 160;
  90.         }
  91.         #endregion
  92.         #region Mouse Events
  93.         private void OnStartMoveKnob(object sender, System.Windows.Input.MouseButtonEventArgs e)
  94.         {
  95.             knob.CaptureMouse();
  96.             knob.PreviewMouseMove += new MouseEventHandler(Knob_PreviewMouseMove);
  97.         }
  98.         private void OnStopMoveKnob(object sender, System.Windows.Input.MouseButtonEventArgs e)
  99.         {
  100.             knob.ReleaseMouseCapture();
  101.             knob.PreviewMouseMove -= new MouseEventHandler(Knob_PreviewMouseMove);
  102.         }
  103.         private void Knob_PreviewMouseMove(object sender, MouseEventArgs e)
  104.         {
  105.             Point delta = GetMouseDelta();
  106.             double yDelta = delta.Y;
  107.             double xDelta = delta.X;
  108.             double angleDelta = Math.Atan2(yDelta, xDelta);
  109.             currentAngle = (angleDelta * 180 / Math.PI);
  110.             if (Math.Abs(currentAngle - previousAngle) < 130) angle += (currentAngle - previousAngle);
  111.             // The knob has a limited range of motion, from -130 and 130
  112.             if ((angle >= -130) && (angle <= 130))
  113.             {
  114.                 // Correction to better align the mouse position with the knob.
  115.                 rotateKnob.Angle = angle - 160;
  116.             }
  117.             else
  118.             {
  119.                 // Keeps the know inside the range.
  120.                 if (angle > 130)
  121.                 {
  122.                     angle = 130;
  123.                 }
  124.                 if (angle < -130)
  125.                 {
  126.                     angle = -130;
  127.                 }
  128.             }
  129.             previousAngle = currentAngle;
  130.             // Converts the angle into the value that is returned.
  131.             // The property notifies the change using PropertyChanged.
  132.             double value = angle / 130;
  133.             if (this.minimum < 0)
  134.             {
  135.                 this.Value = value;
  136.             }
  137.             else
  138.             {
  139.                 double middle = this.maximum / 2;
  140.                 this.Value = middle + (value * middle);
  141.             }
  142.         }
  143.         private Point GetMouseDelta()
  144.         {
  145.             Point mousePos = Mouse.GetPosition(knob);
  146.             Double deltaX = mousePos.X - (knob.Width / 2);
  147.             Double deltaY = mousePos.Y - (knob.Width / 2);
  148.             Point point = new Point(deltaX, deltaY);
  149.             return point;
  150.         }
  151.         #endregion
  152.     }
  153. }